package hz.mobileproject.pda.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.Arrays;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.db.DBManager;
import hz.mobileproject.pda.entity.request.Request_Edu_inter;
import hz.mobileproject.pda.ui.activity.work.EduAllTalkActivity;
import hz.mobileproject.pda.ui.activity.work.EduNoneTalkActivity;
import hz.mobileproject.pda.ui.activity.work.EduUploadActivity;

/**
 * Created by zmybi on 207/8/23.
 */

public class EduTalkActivity extends AppCompatActivity implements View.OnClickListener{

    private UploadFailRefreshPointNumReceiver mReceiver = new UploadFailRefreshPointNumReceiver();
    private ImageView mIvStartEduTalk;
    private TextView mTvHelp;
    private LinearLayout mLlTalkJilu;
    private LinearLayout mLlNoneTalk;
    private LinearLayout mLlDingzhi;
    private TextView mTvUploadFailNum;
    private Banner mBanner;

    private DBManager mDBManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edutalk);
        initView();
        initData();
        initThing();
        registerUploadFailRefreshPointNumReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshUploadPointNum();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    private void registerUploadFailRefreshPointNumReceiver() {
        try{
            IntentFilter filter = new IntentFilter();
            filter.addAction("uploadSuccess");
            registerReceiver(mReceiver, filter);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void refreshUploadPointNum() {
        List<Request_Edu_inter> interList = mDBManager.queryRequest_Edu_inter();
        if(interList.size() <= 0){
            mTvUploadFailNum.setVisibility(View.GONE);
        }else if(interList.size() <= 99){
            mTvUploadFailNum.setVisibility(View.VISIBLE);
            mTvUploadFailNum.setText(String.valueOf(interList.size()));
        }else{
            mTvUploadFailNum.setVisibility(View.VISIBLE);
            mTvUploadFailNum.setText("99+");
        }
    }

    private void initThing() {
        mIvStartEduTalk.setOnClickListener(this);
        mTvHelp.setOnClickListener(this);
        mLlTalkJilu.setOnClickListener(this);
        mLlNoneTalk.setOnClickListener(this);
        mLlDingzhi.setOnClickListener(this);
    }

    private void initData() {
        //设置图片加载器
        mBanner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        Integer[] int_image = {R.drawable.anquan,R.drawable.tanhua};
        List<?> images = Arrays.asList(int_image);
        mBanner.setImages(images);
        //banner设置方法全部调用完毕时最后调用
        mBanner.start();

        mDBManager = DBManager.getInstance(getApplicationContext());
    }

    private void initView() {
        mBanner = (Banner) findViewById(R.id.banner);
        mIvStartEduTalk = (ImageView) findViewById(R.id.iv_start_edu_talk);
        mTvHelp = (TextView) findViewById(R.id.tv_help);
        mLlTalkJilu = (LinearLayout) findViewById(R.id.ll_talk_jilu);
        mLlNoneTalk = (LinearLayout) findViewById(R.id.ll_none_talk);
        mLlDingzhi = (LinearLayout) findViewById(R.id.ll_dingzhi);
        mTvUploadFailNum = (TextView) findViewById(R.id.tv_upload_fail_list_num);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_start_edu_talk:
//                Edu_upload_Activity.launch(EduTalkActivity.this);
                startActivity(new Intent(EduTalkActivity.this,EduUploadActivity.class));
                break;
            case R.id.tv_help:
                ToastUtils.showLong("帮助");
                break;
            case R.id.ll_talk_jilu:
//                startActivity(new Intent(EduTalkActivity.this,AllTalkActivity.class));
                startActivity(new Intent(EduTalkActivity.this,EduAllTalkActivity.class));
                break;
            case R.id.ll_none_talk:
//                startActivity(new Intent(EduTalkActivity.this,NoneTalkPersonActivity.class));
                startActivity(new Intent(EduTalkActivity.this,EduNoneTalkActivity.class));
                break;
            case R.id.ll_dingzhi:
                startActivity(new Intent(EduTalkActivity.this,UploadFailActivity.class));
                break;
        }
    }

    public class GlideImageLoader extends ImageLoader {

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(context).load((Integer) path).into(imageView);
        }
    }

    class UploadFailRefreshPointNumReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){
                case "uploadSuccess" :
                    refreshUploadPointNum();
                    break;
                default:
                    break;
            }
        }
    }
}
